package com.rong.juc_test.juc_base.juc;

import java.util.concurrent.CountDownLatch;

/**
 * 演示CountDownLatch
 */
public class CountDownLatchDemo {
    // 6个同学陆续离开教室后锁门
    public static void main(String[] args) throws InterruptedException {

    // 解决：创建CountDownLatch对象，初始值设置为6
    CountDownLatch countDownLatch = new CountDownLatch(6);

//        创建6个线程
        for(int i=1; i<=6; i++){
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "号同学离开教室");
                // 每个线程打印离开信息后，调用countDown方法，计数器减1
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }

        // 等待
        countDownLatch.await();
        // 存在问题 人没走完，锁门了
        System.out.println(Thread.currentThread().getName() + "班长最后锁门");
    }
}
